package com.sinosoft.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.common.Const;
import com.sinosoft.entity.DefRole;
import com.sinosoft.entity.DefUser;
import com.sinosoft.service.system.IRoleService;

@Controller
@RequestMapping("/system")
public class RoleController {
	@Autowired
	private IRoleService roleService;

	@RequestMapping("/role")
	public ModelAndView role(ModelAndView modelAndView) {
		modelAndView.setViewName("system/role");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("/initRoleTable")
	public Map<String, Object> initRoleTable(DefRole defRole, int page) {
		defRole.setStart((page - 1) * defRole.getRows());
		return roleService.selectRoleByCondition(defRole);
	}
	
	@RequestMapping("/roleAddPage")
	public ModelAndView roleAddPage(ModelAndView modelAndView) {
		modelAndView.setViewName("system/roleAdd");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("/roleAdd")
	public Map<String, Object> roleAdd(DefRole role, HttpServletRequest request) {
		DefRole r = new DefRole();
		r.setRoleCode(role.getRoleCode());
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> m = roleService.selectRoleByCondition(r);
		Object rOj = m.get("total");
		if (null != rOj && (Integer) rOj > 0) {
			map.put("flag", false);
			map.put("msg", "角色添加失败！角色编码已经存在！");
			return map;
		}
		
		DefUser user = (DefUser) request.getSession().getAttribute(Const.SESSION_USER_KEY);
		role.setCreateUserId(user.getUserId());
		role.setModifyUserId(user.getUserId());
		try {
			map = roleService.roleAdd(role);
		} catch (Exception e) {
			map.put("flag", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}
	
	@RequestMapping("/roleModifyPage")
	public ModelAndView roleModifyPage(ModelAndView modelAndView) {
		modelAndView.setViewName("system/roleModify");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("/roleModify")
	public Map<String, Object> roleModify(DefRole role, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		DefUser user = (DefUser) request.getSession().getAttribute(Const.SESSION_USER_KEY);
		role.setModifyUserId(user.getUserId());
		try {
			map = roleService.roleModify(role);
		} catch (Exception e) {
			map.put("flag", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/roleDelete")
	public Map<String, Object> roleDelete(Long roleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = roleService.roleDelete(roleId);
		} catch (Exception e) {
			map.put("flag", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}
}
